package com.wesley.growth.itext7.chapter3;

import cn.hutool.core.io.FileUtil;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


/**
 * NewYorkTimes
 *
 * @author WangPanYong
 * @since 2023/04/07 15:20
 */
public class NewYorkTimes {
    public static final String DEST = "results/chapter03/new_york_times.pdf";

    public static final String APPLE_IMG = "itext7-samples/src/main/resources/img/ny_times_apple.jpg";
    public static final String APPLE_TXT = "itext7-samples/src/main/resources/data/ny_times_apple.txt";
    public static final String FACEBOOK_IMG = "itext7-samples/src/main/resources/img/ny_times_fb.jpg";
    public static final String FACEBOOK_TXT = "itext7-samples/src/main/resources/data/ny_times_fb.txt";
    public static final String INST_IMG = "itext7-samples/src/main/resources/img/ny_times_inst.jpg";
    public static final String INST_TXT = "itext7-samples/src/main/resources/data/ny_times_inst.txt";


    static PdfFont timesNewRoman = null;
    static PdfFont timesNewRomanBold = null;

    void render(File pdfFile) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(pdfFile));

        PageSize ps = PageSize.A5;
        Document document = new Document(pdf, ps);

        //Set column parameters
        // offSet 使用这个变量来定义上下左右边界的宽度
        float offSet = 36;
        // 每一列的宽度，columnWidth,这个通过划分可计算页面成3份（我们的目标是3列）的方式来计算，
        // 可计算页面的大小是 整个页面宽度- 2*offset + 10，其中 +10 的作用是确保每一列之间有空隙
        float columnWidth = (ps.getWidth() - offSet * 2 + 10) / 3;
        // 简单的 整个页面高度 - 2*offset
        float columnHeight = ps.getHeight() - offSet * 2;

        //Define column areas
        // 牢记默认坐标系是在左下角，向右为x轴，向上为y轴
        Rectangle[] columns = {
                // 左下角坐标为 (X = offSet - 5, Y = offSet), width columnWidth and height columnHeight,
                new Rectangle(offSet - 5, offSet, columnWidth, columnHeight),
                // 左下角坐标为 (X = offSet + columnWidth, Y = offSet)
                new Rectangle(offSet + columnWidth, offSet, columnWidth, columnHeight),
                // 左下角坐标为 (X = offSet + columnWidth * 2 + 5, Y = offSet)
                new Rectangle(
                        offSet + columnWidth * 2 + 5, offSet, columnWidth, columnHeight)};
        // 向Document添加的所有内容都会按照我们定义的三个Rectangle的布局来显示
        document.setRenderer(new ColumnDocumentRenderer(document, columns));

        Image apple = new Image(ImageDataFactory.create(APPLE_IMG)).setWidth(columnWidth);
        String articleApple = Files.readString(Paths.get(APPLE_TXT));
        addArticle(document, "Apple Encryption Engineers, if Ordered to Unlock iPhone, Might Resist", "By JOHN MARKOFF MARCH 18, 2016", apple, articleApple);
        Image facebook = new Image(ImageDataFactory.create(FACEBOOK_IMG)).setWidth(columnWidth);
        String articleFB = Files.readString(Paths.get(FACEBOOK_TXT));
        addArticle(document, "With \"Smog Jog\" Through Beijing, Zuckerberg Stirs Debate on Air Pollution", "By PAUL MOZUR MARCH 18, 2016", facebook, articleFB);
        Image inst = new Image(ImageDataFactory.create(INST_IMG)).setWidth(columnWidth);
        String articleInstagram = Files.readString(Paths.get(INST_TXT));
        addArticle(document, "Instagram May Change Your Feed, Personalizing It With an Algorithm","By MIKE ISAAC MARCH 15, 2016", inst, articleInstagram);
        document.close();
    }

    public static void addArticle(
            Document doc, String title, String author, Image img, String text)
            throws IOException {
        Paragraph p1 = new Paragraph(title)
                .setFont(timesNewRomanBold)
                .setFontSize(14);
        doc.add(p1);
        doc.add(img);
        Paragraph p2 = new Paragraph()
                .setFont(timesNewRoman)
                .setFontSize(7)
                .setFontColor(DeviceGray.GRAY)
                .add(author);
        doc.add(p2);
        Paragraph p3 = new Paragraph()
                .setFont(timesNewRoman)
                .setFontSize(10)
                .add(text);
        doc.add(p3);
    }


    public static void main(String[] args) throws IOException {
        timesNewRoman = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        timesNewRomanBold = PdfFontFactory.createFont(StandardFonts.TIMES_BOLD);
        File dir = new File("temp");
        File pdfFile = FileUtil.createTempFile("ny_times_", ".pdf", dir, true);
        new NewYorkTimes().render(pdfFile);
        System.out.println("pdfFile = " + pdfFile.getName());
    }
}
